<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar left-arrow title="编辑资料" @click-left="gotoMy"></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="showNameDialog"
      />

      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender === 0 ? '男' : '女'"
        @click="showSex = true"
      >
      </van-cell>
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="showBirthday"
      />
    </van-cell-group>
    <!-- 昵称修改 -->
    <van-dialog
      @confirm="saveName"
      v-model="showName"
      title="修改昵称"
      show-cancel-button
    >
      <van-field ref="nameField" v-model="name" placeholder="请输入昵称" />
    </van-dialog>
    <!-- 选择性别 -->
    <van-popup
      v-model="showSex"
      round
      position="bottom"
      :style="{ height: '20%' }"
    >
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showSex = false"
      />
      <van-cell is-link title="男" value="" @click="changeSex(0)"> </van-cell>
      <van-cell is-link title="女" value="" @click="changeSex(1)"> </van-cell>
    </van-popup>
    <!-- 修改日期 -->
    <van-popup v-model="showDate" position="bottom">
      <van-datetime-picker
        v-model="birthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="confirmDate"
        @cancel="cancelDate"
      />
    </van-popup>
    <!-- 文件上传 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        fixed
        autoCropWidth="120px"
        autoCropHeight="120px"
        autoCrop
      ></vueCropper>
      <van-button @click="savePhoto" class="confirm-btn" type="primary"
        >确认</van-button
      >
      <van-button @click="showCropper = false" class="cancel-btn" type="primary"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { VueCropper } from 'vue-cropper'

import { editUserInfo, uploadPhoto } from '../../api/edit'
import moment from 'moment'
export default {
  name: 'edit',
  data () {
    return {
      showName: false,
      name: '',
      showSex: false,
      showDate: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      birthday: new Date(2021, 5, 26),
      img: '',
      showCropper: false
    }
  },
  methods: {
    savePhoto () {
      this.$refs.cropper.getCropBlob(blob => {
        const formData = new FormData()
        formData.append('photo', blob)
        uploadPhoto(formData).then(res => {
          // console.log('res', res)
          this.$toast.success('上传成功！')
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.showCropper = false
        })
      })
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      console.log(file)
      this.showCropper = true
      this.img = file.content
      // const formData = new FormData()
      // formData.append('photo', file.file)
      // uploadPhoto(formData).then(res => {
      //   console.log('res', res)
      //   this.$toast.success('上传成功！')
      //   this.$store.commit('setUserInfo', {
      //     ...this.userInfo,
      //     photo: res.data.data.photo
      //   })
      // })
    },
    confirmDate () {
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.$toast.success('更新成功')
        this.showDate = false
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: res.data.data.birthday
        })
      })
    },
    cancelDate () {
      this.showDate = false
    },
    showBirthday () {
      this.showDate = true
      this.birthday = new Date(this.userInfo.birthday)
    },
    changeSex (gender) {
      editUserInfo({ gender }).then(res => {
        this.$toast.success('性别修改成功！')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender
        })
        this.showSex = false
      })
    },
    saveName () {
      editUserInfo({ name: this.name }).then(res => {
        console.log(res)
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: res.data.data.name
        })
      })
    },
    gotoMy () {
      this.$router.push({ path: '/my' })
    },
    showNameDialog () {
      this.name = this.userInfo.name
      this.showName = true
      this.$nextTick(() => {
        this.$refs.nameField.focus()
      })
    }
  },
  computed: mapState(['userInfo']),
  components: {
    VueCropper
  }
  // computed: {
  //   userInfo () {
  //     return this.$store.state.userInfo
  //   }
  // }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  // .van-nav-bar {
  //   background-color: #3196fa;
  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  //   .van-icon-arrow-left {
  //     color: #fff;
  //   }
  //   .van-nav-bar__text {
  //     color: #fff;
  //   }
  // }
  // 头像部分
  .avatar {
    position: relative;
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
    .van-uploader {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      height: 120px;
      width: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
}
.van-dialog {
  padding: 10px 20px;
  box-sizing: border-box;
  .van-field {
    border: 1px solid #ccc;
  }
}
.cropper-box {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .confirm-btn {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .cancel-btn {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
